<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_pingdd_pin/model')->方法名();
*/
namespace jy_pingdd_pin;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	/*
		确认有没有活得佣金的权限
	*/
	public function checkcanfee($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$basic=$this->plugin_setting('basic');
		if($basic['is_daycanyu']==1){
			$daytime=mktime(0,0,0,date('m'),date('d'),date('Y'));
			$iscan=pdo_count('jy_weishop_order',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_pingdd_pin','createtime >='=>$daytime,'status >'=>0));
			if($iscan>0){
				return true;
			}else{
				return false;
			}
		}else{
			return true;
		}
	}
	/*
		确认拼单
	*/
	public function checkteam($goodsid,$teamid=0){
		$basic=$this->plugin_setting('basic');
		if (!$teamid && !$basic['is_morepin']) {
			//判断团长是否是首次开团
			$hasteam=pdo_get('jy_weishop_groups_team',array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_pingdd_pin','status'=>0,'goodsid'=>$goodsid,'mid'=>$_SESSION['uid']));
			if($hasteam['id']){
				exi('您有一个该商品的拼单还在进行中！','error','jy_pingdd_pin/teamdetail//teamid='.$hasteam['id']);
			}
		}
		$order=pdo_get('jy_weishop_order',array('mid'=>$_SESSION['uid'],'plugin_name'=>'jy_pingdd_pin','teamid'=>$teamid,'status'=>10,'uniacid' => $_SESSION['uniacid']));
		if ($teamid && $order['id'] && !$basic['is_morepin']) {
			exi('您的该商品拼单已存在！','error','jy_pingdd_pin/myteam/');
		}
		if ($teamid) {
			$team = pdo_get('jy_weishop_groups_team',array('id'=>$teamid,'uniacid' => $_SESSION['uniacid']));
			if ($team['status']){
				exi('该拼单已完成或过期，请参与其他拼单！','error','jy_pingdd_pin/index/');
			}
		}
		return true;
	}
	/*
		拼团失败
	*/
	public function order_cancel($teamid){
		$params=array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_pingdd_pin','teamid'=>$teamid,'status'=>10);
		$order=pdo_getall('jy_weishop_order',$params,array('id','mid','teamid','price','paytype'));
		foreach($order as $item){
			$this->order_choufail($item);
		}
		pdo_update('jy_weishop_groups_team',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$teamid));
	}
	/*
		拼团成功
	*/
	public function order_success($teamid){
		pdo_update('jy_weishop_groups_team',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$teamid));
		$data=array(
			'status'=>1,
		);
		$goodsid=pdo_getcolumn('jy_weishop_groups_team',array('uniacid'=>$_SESSION['uniacid'],'id'=>$teamid),'goodsid');
		$orders=pdo_getall('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_pingdd_pin','teamid'=>$teamid,'status'=>10));
		//抽中
		$pin_goods=pdo_get('jy_pingdd_pin_goods',array('goodsid'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
		foreach($orders as $k=>$ov){
			//看还可以中不？
			$canzhong=$this->check_canzhong($pin_goods['id'],$ov['mid']);
			if(!$canzhong){
				$this->iszhong_get($ov,$pin_goods);
				unset($orders[$k]);
			}
		}
		$orders=array_values($orders);
		$num=count($orders);
		for($i=1;$i<=$pin_goods['lastnum'];$i++){
			$key=mt_rand(0,$num-$i);
			if(!$orders[$key]['id']){
				$key=0;
			}
			$orders[$key]['pingid']=$pin_goods['id'];
			//拼中奖励
			if($pin_goods['zhong_fee']>0 && $orders[$key]['id']){
				$orders[$key]['zhong_fee']=$pin_goods['zhong_fee'];
				$orders[$key]['pingid']=$pin_goods['id'];
				$this->order_zhong_fee($orders[$key],$goodsid);
			}
			$this->order_getsuccess($orders[$key],$goodsid);
			unset($orders[$key]);
			$orders=array_values($orders);
		}		
		foreach($orders as $ov){
			$ov['goodsid']=$goodsid;
			$ov['pin_goods']=$pin_goods;
			if($pin_goods['allget']==1){
				$ov['pingid']=$pin_goods['id'];
				$this->order_getsuccess($ov,$goodsid);
			}else{
				$this->order_choufail($ov);
			}
			$this->order_fanxian($ov);
		}
		return true;
	}
	public function order_getsuccess($item,$goodsid=0){
			$data=array(
				'status'=>1,
			);
			pdo_update('jy_weishop_order',$data,array('id'=>$item['id']));
			$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid));
			if(!$item['mid']){
				return false;
			}
			$item['finishtime']=TIMESTAMP;
			$item['goodstitle']=$goods['title'];
			$item['orderprice']=$item['price'];
			model('jy_weishop/notice')->notice_groupfinish_ding($item);
			$params=array(
				'pingid'=>$item['pingid'],
				'msg'=>'拼团成功！',
				'status'=>1,
				'mid'=>$item['mid'],
			);
			$this->set_log($params,3);
			return true;
	}
	public function order_choufail($item){
			$check=model('jy_weishop/order/refund')->check_order_rerefund_pin(intval($item['id']));
			if(!$check){
				return false;
			}
			$params=array(
				'orderid'=>intval($item['id']),
				'images'=>$item['images']?ijson_encode($item['images']):'',
				'price'=>$item['price'],
				'plugin_name'=>'jy_pingdd_pin',
				'content'=>'拼单失败退款',
			);
			$basic=$this->plugin_setting('basic');
			$refund_id=model('jy_weishop/order/refund')->refund_create($params,true);
			if($basic['is_zdrefund']==1 && $refund_id){
				model('jy_weishop/order/refund')->refund_pay($refund_id,true,true,'credit');
			}
			model('jy_weishop/notice')->notice_groupfail($item);
			return true;
	}
	/*
		支付成功返现
	*/
	public function order_fanxian($order,$pluginset=array()){
		//$pluginset=$pluginset?:$this->plugin_setting('basic');
		$teamgoods=pdo_get('jy_pingdd_pin_goods',array('goodsid'=>$order['goodsid'],'uniacid'=>$_SESSION['uniacid']));
		$fanfee=$teamgoods['fan_fee'];
		if($fanfee>0){
			model('core/member')->add_credit2($order['mid'],$fanfee,'拼单参与奖励','jy_pingdd_pin');
			$params=array(
				'pingid'=>$order['pingid'],
				'msg'=>'拼单参与奖励！',
				'status'=>1,
				'mid'=>$order['mid'],
				'orderid'=>$order['id'],
			);
			$this->set_log($params,7);
			model('jy_weishop/notice')->notice_groupfinish_fan($order,$fanfee);
		}
		return true;
	}
	/*
		拼中奖励
	*/
	public function order_zhong_fee($order,$pluginset=array()){
		//$pluginset=$pluginset?:$this->plugin_setting('basic');
		$zhong_fee=$order['zhong_fee'];
		model('core/member')->add_credit2($order['mid'],$zhong_fee,'拼单拼中额外奖励','jy_pingdd_pin');
		$params=array(
				'pingid'=>$order['pingid'],
				'msg'=>'拼中奖励！',
				'status'=>1,
				'mid'=>$order['mid'],
				'orderid'=>$order['id'],
		);
		$this->set_log($params,8);
		model('jy_weishop/notice')->notice_groupfinish_fan($zhong_fee,$order['id']);
		return true;
	}
	/*
		已经中满了，直接失败
	*/
	public function iszhong_get($order,$pinggoods=array()){
		$order['goodsid']=$pinggoods['goodsid'];
		$order['pin_goods']=$pin_goods;
		if($pin_goods['allget']==1){
				$order['pingid']=$pin_goods['id'];
				$this->order_getsuccess($order,$pinggoods['goodsid']);
		}else{
				$this->order_choufail($order);
		}
		$this->order_fanxian($order);
		return true;
	}
	public function up_price($goods,$pluginset=array(),$isprice=false){
		$teamgoods=pdo_get('jy_pingdd_pin_goods',array('goodsid'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']));
		if($teamgoods['id'] && !$teamgoods['status']){
			exi('选择的商品不存在或已下架！','error','/index/');
		}
		$pluginset=$pluginset?:$this->plugin_setting('basic');
		$goods['ding_price']=$teamgoods['ding_price'];
		if($goods['groupprice']==0){
				$pluginset['groupdiscount']=$pluginset['groupdiscount']?:98;
				$goods['groupprice']=price($goods['price']*$pluginset['groupdiscount']/100);
		}
		if($pluginset['bao_type']==1){
				$goods['ding_price']=$goods['ding_price']?:$pluginset['bao_fee'];
		}else{
				$goods['ding_price']=$goods['ding_price']?:$pluginset['bao_rate']*$goods['price']/100;
		}
		if($pluginset['bao_type']==1){
				$goods['ding_price']=$goods['ding_price']?:$pluginset['bao_fee'];
		}elseif($pluginset['bao_rate']){
				$goods['ding_price']=$goods['ding_price']?:$pluginset['bao_rate']*$goods['price']/100;
		}
		if($pluginset['groupdiscount']){
				$goods['groupprice']=$pluginset['groupdiscount']*$goods['price']/100;
		}
		$goods['grouptime']=$teamgoods['timeout']?:$pluginset['timeout'];
		$goods['grouptime']=$goods['grouptime']?:48;
		$goods['teamnum']=$teamgoods['teamnum']?:$pluginset['teamnum'];
		$goods['teamnum']=$goods['teamnum']?:2;
		$goods['groupprice']=$teamgoods['price']?:$goods['groupprice'];
		$goods['fan_fee']=$teamgoods['fan_fee'];
		$goods['zhong_fee']=$teamgoods['zhong_fee'];
		$goods['lastnum']=$teamgoods['lastnum'];
		$goods['failnum']=$teamgoods['teamnum']-$teamgoods['lastnum'];
		if($isprice){
			$goods['groupprice']=price($goods['groupprice']);
			$goods['fan_fee']=price($goods['fan_fee']);
			$goods['ding_price']=price($goods['ding_price']);
		}
		$goods['fan_price']=$goods['fan_fee'];
		$goods['allget']=$teamgoods['allget'];
		return $goods;
	}
	/*
		判断预约
	*/
	public function set_yuyue($pingid,$addressid=0){
		$yuyue=pdo_get('jy_pingdd_pin_yuyue',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pingid'=>$pingid),'id');
		if($yuyue){
			return false;
		}
		$pinggoods=pdo_get('jy_pingdd_pin_goods',array('id'=>$pingid,'uniacid'=>$_SESSION['uniacid']));
		$item=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','mobile','avatar','nickname'));
		if($pinggoods['price']>$item['credit2']){
			exi('余额不足，当前余额（'.$item['credit2'].'元），不能预约本拼团！','error');
		}
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'pingid'=>$pingid,
			'addressid'=>$addressid,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_pingdd_pin_yuyue',$insert);
		return true;
	}
	/*
		日志信息
	*/
	public function set_log($params,$type=0){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$params['mid']?:$_SESSION['uid'],
			'pingid'=>$params['pingid'],
			'msg'=>$params['msg'],
			'status'=>$params['status'],
			'gift_type'=>$params['gift_type'],
			'other_goodsid'=>$params['other_goodsid'],
			'other_credit_fee'=>$params['other_credit_fee'],
			'type'=>$type,
			'orderid'=>$params['orderid'],
			'dayid'=>date('Y-m-d',TIMESTAMP),
			'merchid'=>$params['merchid'],
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_pingdd_pin_log',$insert);
		return true;
	}
	//获取当前拼单时间
	public function get_times($timeids,$getnext=false,$getcanping=false){
		if(is_array($timeids)){
			
		}else{
			$timeids=str_array($timeids);
		}
		$times=array();
		foreach($timeids as $k=>$time){
			$times[]=array(
				'time'=>$time
			);
		}
		paixu($times,'time','asc');
		foreach($times as $key=>&$tv){
			$startime = strtotime(date('Y-m-d ' . $tv['time'].':00'));
			$endtv=$tv['time']+1;
			$endtime = strtotime(date('Y-m-d ' . $endtv.':00'));
			$tv['time']=$tv['time'].':00';
			if($startime>TIMESTAMP){
				if($getnext){
					return $tv['time'];
				}
				$status=0;
			}elseif($startime<TIMESTAMP && $endtime<TIMESTAMP){
				$status=2;
			}else{
				$canping=1;
				$status=1;
				$timeid=$tv['time'];
			}
			$tv['endtime']=$endtv;
			$tv['status']=$status;
		}
		if($getcanping){
			if(count($timeids)){
				return $canping;	
			}else{
				return 1;
			}
			
		}elseif($getnext){
			return false;
		}else{
			return $times;
		}
	}
	//确认当前时段是否已拼单
	public function che_isping($pingid,$mid,$timeid){
		$params=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'pingid'=>$pingid,
			'type'=>0,
			'dayid'=>date('Y-m-d',TIMESTAMP),
		);
		$isping=pdo_get('jy_pingdd_pin_log',$params);
		if($isping['id']){
			return true;
		}else{
			return false;
		}
	}
	//确认用户
	public function check_canzhong($pingid,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$pinggoods=pdo_get('jy_pingdd_pin_goods',array('id'=>$pingid,'uniacid'=>$_SESSION['uniacid']));
		$num=$this->get_orderzhongnum($pingid,$mid);
		if($num>=$pinggoods['jiateamzhong']){
			return false;
		}
		return true;
	}
	//确认用户命中个数
	public function get_orderzhongnum($pingid,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$num=pdo_count('jy_pingdd_pin_log',array('dayid'=>date('Y-m-d',TIMESTAMP),'pingid'=>$pingid,'mid'=>$mid,'type'=>3,'status'=>1),'id');
		return $num;
	}
	/*
		额外中奖
	*/
	public function orderzhong($orders,$pin_goods=array()){
		foreach($orders as $item){
			if($pin_goods['other_share']){
				$other_share=$this->get_agentnum($item['mid']);
				if($other_share<$pin_goods['other_share']){
					return false;
				}
			}
			$zhong=mt_rand(0,100);
			if($zhong<=$pin_goods['teamnum']){
				$params=array(
					'pingid'=>$pin_goods['id'],
					'msg'=>'恭喜获得额外中奖！',
					'status'=>1,
					'gift_type'=>$pin_goods['gift_type'],
					'other_goodsid'=>$pin_goods['other_goodsid'],
					'other_credit_fee'=>$pin_goods['other_credit_fee'],
					'mid'=>$item['mid'],
				);
				$this->set_log($params,4);
			}
		}
		return true;
	}
	/*
		推荐N个人
	*/
	public function get_agentnum($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$mids=pdo_getall('core_members',array('fromuser'=>$mid,'uniacid'=>$_SESSION['uniacid']),'id','id');
		$num=0;
		foreach($mids as $item){
			$pingnum=pdo_count('jy_pingdd_pin_log',array('type'=>0,'status'=>1,'mid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
			if($pingnum){
				$num++;
			}
		}
		return $num;
	}
}
?>